[XEND] Ignore socket.shutdown() exceptions
authorAlastair Tse <atse@xensource.com>
Thu, 25 Jan 2007 14:52:36 +0000 (14:52 +0000)
committerAlastair Tse <atse@xensource.com>
Thu, 25 Jan 2007 14:52:36 +0000 (14:52 +0000)
Add further comments about why that shutdown() exists.

Signed-off-by: Alastair Tse <atse@xensource.com>
tools/python/xen/web/httpserver.py
tools/python/xen/xend/server/XMLRPCServer.py

index 14965e418fa296c87e4a9d140ad50a0d7fd25ab4..d8a1f78a563e6a16a514f9d5de8807610d20e238 100644 (file)
@@ -333,11 +333,15 @@ class HttpServer:
     def close(self):
         self.closed = True
         self.ready = False
+        # shutdown socket explicitly to allow reuse
+        try:
+            self.socket.shutdown(2)
+        except socket.error:
+            pass
+
         try:
-            # shutdown socket explicitly to allow reuse
-            self.socket.shutdown(socket.SHUT_RDWR)
             self.socket.close()
-        except:
+        except socket.error:
             pass
 
     def getServerAddr(self):
index 64895104be84516cb1c6ed4e15a6351e8d07d30f..722ccb4b2b4c451c08ca22d9736e6b88a2a0dc53 100644 (file)
@@ -188,14 +188,22 @@ class XMLRPCServer:
 
     def cleanup(self):
         log.debug('XMLRPCServer.cleanup()')
-        try:
-            if hasattr(self, 'server'):
-                # shutdown socket explicitly to allow reuse
-                self.server.socket.shutdown(socket.SHUT_RDWR)
+        if hasattr(self, 'server'):
+            try:
+                # This is here to make sure the socket is actually
+                # cleaned up when close() is called. Otherwise
+                # SO_REUSEADDR doesn't take effect. To replicate,
+                # try 'xend reload' and look for EADDRINUSE.
+                #
+                # May be caued by us calling close() outside of
+                # the listen()ing thread.
+                self.server.socket.shutdown(2)
+            except socket.error, e:
+                pass # ignore any socket errors
+            try:
                 self.server.socket.close()
-        except Exception, exn:
-            log.exception(exn)
-            pass
+            except socket.error, e:
+                pass
 
     def shutdown(self):
         self.running = False